package com.qs.questionnairesurveysystem.controller;

import com.qs.questionnairesurveysystem.dto.Result;
import com.qs.questionnairesurveysystem.entity.Question;
import com.qs.questionnairesurveysystem.service.QuestionService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/questions")
@Api(tags = "Question Controller")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @ApiOperation(value = "Create a new question", notes = "Creates a new question in the system.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Question created successfully."),
            @ApiResponse(code = 400, message = "Invalid input data.")
    })
    @PostMapping
    public Result<Question> createQuestion(@RequestBody Question question) {
        questionService.createQuestion(question);
        return Result.success(question);
    }

    @ApiOperation(value = "Update an existing question", notes = "Updates an existing question in the system.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Question updated successfully."),
            @ApiResponse(code = 404, message = "Question not found.")
    })
    @PutMapping("/{questionId}")
    public Result<Question> updateQuestion(@PathVariable Long questionId, @RequestBody Question question) {
        question.setQuestionId(questionId);
        questionService.updateQuestion(question);
        return Result.success(question);
    }

    @ApiOperation(value = "Delete a question", notes = "Deletes a question from the system.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Question deleted successfully."),
            @ApiResponse(code = 404, message = "Question not found.")
    })
    @DeleteMapping("/{questionId}")
    public Result<String> deleteQuestion(@PathVariable Long questionId) {
        questionService.deleteQuestion(questionId);
        return Result.success("Question deleted successfully.");
    }

    @ApiOperation(value = "Get a specific question by ID", notes = "Fetches a question by its ID.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Question retrieved successfully."),
            @ApiResponse(code = 404, message = "Question not found.")
    })
    @GetMapping("/{questionId}")
    public Result<Question> getQuestionById(@PathVariable Long questionId) {
        Question question = questionService.getQuestionById(questionId);
        return question != null ? Result.success(question) : Result.error("not found", 404);
    }

    @ApiOperation(value = "Get all questions", notes = "Fetches all questions from the system.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Questions retrieved successfully.")
    })
    @GetMapping
    public Result<List<Question>> getAllQuestions() {
        List<Question> questions = questionService.getAllQuestions();
        return Result.success(questions);
    }

    @ApiOperation(value = "Get questions by survey ID", notes = "Fetches questions that belong to a specific survey.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Questions retrieved successfully.")
    })
    @GetMapping("/survey/{surveyId}")
    public Result<List<Question>> getQuestionsBySurveyId(@PathVariable Long surveyId) {
        List<Question> questions = questionService.getQuestionsBySurveyId(surveyId);
        return Result.success(questions);
    }
}
